﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SG.Logic.Stock
{
   
public class Hastings {

    private static double f;
    private static double xp;
    private static double error;
    private static double p;
    private static double t;
    private static double lim;
    private static  double C0 = 2.515517;
    private static  double C1 = 0.802853;
    private static  double C2 = 0.010328;
    private static  double D1 = 1.432788;
    private static  double D2 = 0.189269;
    private static  double D3 = 0.001308;
    
    public static double invNormal(double nivelServicio){
        
        lim = 0.000001;
        error = 1.0;
        f = nivelServicio;
        p = f;
        if (f > 0.5) p = 1-f;
        if (Math.Abs(f-p)<lim) return 0;
        else{
            error = 0;
            t = Math.Sqrt(-2*Math.Log(p));
            xp = t - (C0+C1*t+C2*Math.Pow(t, 2))/(1+D1*t+D2*Math.Pow(t, 2)+D3*Math.Pow(t, 3));
            if (f<0.5) xp = -xp;
            return xp;            
        }
        
    }
}

}
